package cc.lj.internal.coupons.domain.ticket.service.impl.useTicketRule;

import cc.lj.internal.api.ordershare.dto.OrderOnlineAPIDTO;
import cc.lj.internal.api.ordershare.dto.OrderPaymentAPIDTO;
import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketMainDO;
import cc.lj.internal.coupons.domain.ticket.repository.IUseTicketCheckCacheRepository;
import cc.lj.internal.coupons.domain.ticket.service.AbstractUseTicketRuleDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.interfaces.dto.command.UseTicketCheckCommand;
import lj.internal.dict.enums.ticketBatch.EnumTicketLadderStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用券验证规则 - 阶梯累加规则
 * @author liangxifeng
 * @date 2023/6/26 15:12
 */
@Service("useTicketMostAddRuleDomainServiceImpl")
@Slf4j
public class UseTicketMostAddRuleDomainServiceImpl extends AbstractUseTicketRuleDomainService {
    //用券验证 redis 缓存仓储接口
    @Resource(name = "usetTicketCheckCacheRedisRepositoryImpl")
    private IUseTicketCheckCacheRepository cacheRepository;

    @Override
    protected void rule(TicketBatchDO batchDO, TicketMainDO ticketMainDO, UseTicketCheckCommand ticketCheckCommand, OrderOnlineAPIDTO orderOnlineAPIDTO) {
        log.info("【电子券用券验证】：阶梯累加规则验证开始==");
        if(ticketCheckCommand.getUuId() == null) throw new ServiceException(CoupousExceptionEnum.USE_TICKET_CHECK_UUID_IS_NULL);

        //1.从redis中读取券批次使用数量 <券批次主键，已用券数量>
        Map<Integer,Integer> usedBatchNumHash = cacheRepository.getUseBatch(ticketCheckCommand.getPactNum(),ticketCheckCommand.getUuId());
        Integer usedTicketNum = 0; //已用券数量
        //如果从redis中能够取到券批次 对用 已使用数量, 则将 已用券数量 赋值
        if(usedBatchNumHash != null) {
            if (usedBatchNumHash.get(batchDO.getBatchId()) != null) {
                if (usedBatchNumHash.get(batchDO.getBatchId()) > 0){
                    usedTicketNum = usedBatchNumHash.get(batchDO.getBatchId());
                }
            }
        }
        log.info("用券累加规则验证：券批次batchId:"+batchDO.getBatchId()+",已用券张数:"+usedTicketNum+"，可用券张数:"+batchDO.getConditionNumber());
        //2.如果不允许阶梯累加 and 当前券批次已用券张数 >= 可用张数
        if(batchDO.getConditionType() == EnumTicketLadderStatus.NO.getCode() && usedTicketNum >= batchDO.getConditionNumber()) {
            log.error("用券累加规则验证：该批次券batchId:"+batchDO.getBatchId()+",不允许阶梯累加使用，已用张数："+usedTicketNum+", 可用张数："+batchDO.getConditionNumber());
            throw new ServiceException(CoupousExceptionEnum.USE_TICKET_MOST_ADD_NOT);
        } else { //允许阶梯累加
            //最多累加张数 > 0 AND 已用张数 >= 最多累加张数   (最多累加张数mostAddNumber=0不做任何处理)
            if(batchDO.getMostAddNumber() > 0 && usedTicketNum >= batchDO.getMostAddNumber() ) {
                log.error("用券累加规则验证：该批次券batchId:"+batchDO.getBatchId()+",超过最多累加张数，已用张数："+usedTicketNum+", 最多累加张数："+batchDO.getMostAddNumber());
                throw new ServiceException(CoupousExceptionEnum.USE_TICKET_MORETHAN_MOST_ADDNUM);
            }
        }
        log.info("【电子券用券验证】：阶梯累加规则验证通过==");
    }

}
